package com.lgkj.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.GoodsListTypeAdapter;
import com.lgkj.bean.meitao.GoodsListTypeBean;
import com.lgkj.ui.meitao.HomeFragment;
import com.lgkj.ui.shop.ShopDetailsActivivty;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/10/12 0012.
 */

public class GoodsListActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.gv_item)
    GridView lvItem;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.iv_title)
    ImageView ivTitle;
    private GoodsListTypeAdapter adapter;
    private GoodsListTypeBean bean;
    int page = 1;
    public static boolean isRefresh = true;
    public static String is_hot = "-1";
    public static String is_new = "-1";
    public static String type_id = "-1";
    public static String reg_level = "-1";
    private int[] img = {R.mipmap.type_title_img1, R.mipmap.type_title_img2, R.mipmap.type_title_img3, R.mipmap.type_title_img4, R.mipmap.type_title_img5, R.mipmap.type_title_img6, R.mipmap.type_title_img7, R.mipmap.type_title_img8};

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_list;
    }

    @Override
    public void initView() {
        setTitle("商品列表");
        adapter = new GoodsListTypeAdapter(GoodsListActivity.this);
        lvItem.setAdapter(adapter);
        lvItem.setOnItemClickListener(this);
        typeImg();
    }

    private void typeImg() {
        try {
            ivTitle.setImageResource(img[HomeFragment.typeTitleImg]);
        } catch (Exception e) {
            Log.i(TAG, "typeImg: " + HomeFragment.typeTitleImg);
        }

    }

    /**
     * http://mda.lianguangtech.com/api/Shop_Dynamic/shopDynamicListByShop?access_token=80a0d224500097b389590ce3d2c2ce17&sid=35d74b9faa1550c010484802469748b1&shop_id=287&shop_table=banbershop
     */
    @Override
    public void initData() {
        srlRefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                initDatas();
            }
        });
        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initDatas();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh)
            initDatas();
    }

    private void initDatas() {
        showDiaLog("正在获取");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(GoodsListActivity.this));
        if (!reg_level.equals("-1"))
            map.put("reg_level", reg_level);
        if (!type_id.equals("-1"))
            map.put("type_id", type_id);
        if (!is_hot.equals("-1"))
            map.put("is_hot", is_hot);
        if (!is_new.equals("-1"))
            map.put("is_new", is_new);
        map.put("page", page + "");
        HttpUtils.postHttp(map, CommonConstant.goodsList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (page == 1) {
                    adapter.dataClean();
                }
                if (jsonObject.optInt("code") == 0) {
                    finishLoadingData(true);
                    Gson mGson = new Gson();
                    bean = mGson.fromJson(String.valueOf(jsonObject), GoodsListTypeBean.class);
                    if (bean != null && bean.getData() != null && bean.getData().getData() != null) {
                        adapter.setData(bean.getData().getData());
                    }
                } else {
                    finishLoadingData(false);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                if (page != 1) {
                    page--;
                    srlRefresh.finishLoadmore(false);
                } else {
                    srlRefresh.finishRefresh(false);
                }
            }
        });
    }

    /**
     * 结束刷新数据
     */
    private void finishLoadingData(boolean succ) {
        if (page == 1) {
            srlRefresh.finishRefresh(succ);
        } else {
            srlRefresh.finishLoadmore(succ);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String goodsId = adapter.getItem(position).getId();
        Intent intent = new Intent(new Intent(GoodsListActivity.this, ShopDetailsActivivty.class));
        intent.putExtra("goodsId", goodsId);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
